package com.happok.onlineclass.usermanagement.user.controller;

import com.alibaba.fastjson.JSON;
import com.happok.common.result.CommonResult;
import com.happok.onlineclass.usermanagement.common.dto.IdDTO;
import com.happok.onlineclass.usermanagement.user.dto.AddUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/7/29 16:23
 */
@AutoConfigureMockMvc
public class UserCommon {
    @Autowired
    private static MockMvc mockMvc;

    public static void setMockMvc(MockMvc mockMvc) {
        UserCommon.mockMvc = mockMvc;
    }

    public static Integer addUser(AddUserDTO addUserDTO) throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/user-management/management/users/v1")
                .contentType(MediaType.APPLICATION_JSON)
                .content(JSON.toJSON(addUserDTO).toString())
                .header("user", "ewoiaWQiOjEsCiJuaWNrTmFtZSI6IumDreWul+S6riIKfQ"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk()).andReturn();

        CommonResult data = JSON.parseObject(result.getResponse().getContentAsString(), CommonResult.class);
        IdDTO idDTO = JSON.parseObject(data.getData().toString(), IdDTO.class);
        return idDTO.getId();
    }

}